package com.developer.platform.controller.system;

import com.developer.platform.application.command.MenuCmdManager;
import com.developer.platform.application.command.cmd.MenuAdd;
import com.developer.platform.application.command.cmd.MenuUpdate;
import com.developer.platform.application.query.MenuQueryManager;
import com.developer.platform.application.query.dto.MenuListDto;
import com.developer.platform.application.query.dto.MenuParentDto;
import com.developer.platform.application.query.dto.MenuTreeDto;
import com.developer.platform.application.query.dto.RoleMenuDto;
import com.developer.platform.controller.BaseController;
import com.developer.platform.infrastructure.enums.StatusEnum;
import com.veeker.log.annotations.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ：qiaoliang
 * @date ：2020-11-09
 */
@Api(tags = "资源管理")
@Validated
@RequestMapping("/system/menu")
@RestController
public class MenuController extends BaseController {

    private final MenuQueryManager menuQueryManager;
    private final MenuCmdManager menuCmdManager;

    public MenuController(MenuQueryManager menuQueryManager,
                          MenuCmdManager menuCmdManager) {
        this.menuQueryManager = menuQueryManager;
        this.menuCmdManager = menuCmdManager;
    }

    @ApiImplicitParams({
            @ApiImplicitParam(value = "资源标题", name = "title", required = false),
            @ApiImplicitParam(value = "路由地址", name = "component", required = false),
            @ApiImplicitParam(value = "状态", name = "status", required = false),
            @ApiImplicitParam(value = "页大小", name = "pageSize", required = true),
            @ApiImplicitParam(value = "当前页", name = "pageNumber", required = true),
    })
    @Log(businessType = "资源管理",discription = "查询资源信息列表")
    @ApiOperation(value = "查询资源信息列表")
    @GetMapping
    public ResponseEntity<List<MenuListDto>> list(@RequestParam(value = "title" , required = false) String title,
                                                  @RequestParam(value = "component" , required = false) String component,
                                                  @RequestParam(value = "status" , required = false) StatusEnum status){
        return ResponseEntity.ok(menuQueryManager.list(title, component, status));
    }

    @Log(businessType = "资源管理",discription = "查询资源列表及角色包含资源")
    @ApiOperation(value = "查询资源列表及角色包含资源")
    @GetMapping("/role/{roleId}")
    public ResponseEntity<RoleMenuDto> treeRoleId(@PathVariable(value = "roleId") long roleId){
        return ResponseEntity.ok(menuQueryManager.getRoleMenuDto(roleId));
    }

    @Log(businessType = "资源管理",discription = "查询树结构资源")
    @ApiOperation(value = "查询树结构资源")
    @GetMapping("/tree")
    public ResponseEntity<List<MenuTreeDto>> tree(){
        return ResponseEntity.ok(menuQueryManager.getMenuTree());
    }

    @Log(businessType = "资源管理",discription = "查询可分配资源")
    @ApiOperation(value = "查询可分配资源")
    @GetMapping("/parent")
    public ResponseEntity<List<MenuParentDto>> parent(){
        return ResponseEntity.ok(menuQueryManager.parent());
    }

    @Log(businessType = "资源管理",discription = "新增资源信息")
    @ApiOperation(value = "新增资源信息")
    @PostMapping
    public ResponseEntity<?> save(@Valid @RequestBody MenuAdd menuAdd){
        menuCmdManager.save(menuAdd);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    @Log(businessType = "资源管理",discription = "修改资源信息")
    @ApiOperation(value = "修改资源信息")
    @PutMapping("/{id}")
    public ResponseEntity<?> save(@PathVariable("id") long id ,
                                  @Valid @RequestBody MenuUpdate menuUpdate){
        menuCmdManager.update(id, menuUpdate);
        return ResponseEntity.status(HttpStatus.RESET_CONTENT).build();
    }

    @Log(businessType = "资源管理",discription = "删除资源信息")
    @ApiOperation(value = "删除资源信息")
    @DeleteMapping("/{id}")
    public ResponseEntity<?> delete(@PathVariable("id") long id){
        menuCmdManager.deleteById(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

}
